/*
 * ListenerNuevoCd.java
 *
 * Created on 28 de octubre de 2005, 1:22
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package fmap.gui.actionListener;

import fmap.DatosCompartidos;
import fmap.dao.GroupFileDao;
import fmap.gui.JFrameGroupFile;
import fmap.model.GroupFile;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author javi
 */
public class ListenerJFrameGroupFile implements ActionListener,ListSelectionListener
{
    JFrameGroupFile jFrameGroupFile;
    GroupFile groupFileSelect;
    
    /** Creates a new instance of ListenerNuevoCd */
    public ListenerJFrameGroupFile ( JFrameGroupFile _jFrameGroupFile )
    {
        jFrameGroupFile = _jFrameGroupFile;
    }
    
    
    
    public void valueChanged (ListSelectionEvent event)
    {
        JList listGroupFile   = (JList)event.getSource ();
        groupFileSelect     = (GroupFile)listGroupFile.getSelectedValue ();
        
        if( groupFileSelect != null )
        {
            jFrameGroupFile.getTextFNameGroupFile ().setText ( groupFileSelect.getName () );
            jFrameGroupFile.getTextAExtensionGroupFile ().setText ( groupFileSelect.getExtension () );
        }
    }
    
    
    public void actionPerformed (ActionEvent ae)
    {
        JButton jb = (JButton)ae.getSource ();
        
        // Insertamos una nueva categoria
        if( jb.getActionCommand ().equals ("guardar") )
        {
            
            
            if( !jFrameGroupFile.getTextFNameGroupFile ().getText ().trim ().equals ("") )
            {
                // Insertamos un nuevo grupo de ficheros
                if( groupFileSelect == null)
                {
                    new GroupFileDao ().insertGroupFile ( jFrameGroupFile.getTextFNameGroupFile ().getText (), jFrameGroupFile.getTextAExtensionGroupFile ().getText () );
                }
                else // Actualizamos un grupo de ficheros existente
                {
                    new GroupFileDao ().updateGroupFile ( groupFileSelect.getId (), jFrameGroupFile.getTextFNameGroupFile ().getText (), jFrameGroupFile.getTextAExtensionGroupFile ().getText () );
                }
                
                // Actualizamos la lista de categorias
                Vector vGroupFile = new GroupFileDao ().selectGroupFile ();
                if( vGroupFile.size () > 0 ) jFrameGroupFile.getListGroupFile ().setListData ( vGroupFile );

                // Actualizamos el combo del jframe de la busqueda avanzada
                // Este codigo deberia estar en un action
                DefaultComboBoxModel modelComboBox = new DefaultComboBoxModel ();
                modelComboBox.addElement ( DatosCompartidos.getGroupFileSinFiltro () );
                for( int i=0; i<vGroupFile.size (); i++)
                {
                    modelComboBox.addElement ( vGroupFile.get (i) );
                }

                DatosCompartidos.getJFrameSearchAdvanced ().getComboBoxGroupFileSearch ().setModel ( modelComboBox );

                jFrameGroupFile.getTextFNameGroupFile ().setText ("");
                jFrameGroupFile.getTextAExtensionGroupFile ().setText ("");
            }
            else
            {
                JOptionPane.showMessageDialog (jFrameGroupFile,
                        "Debe cubrir el nombre del grupo de ficheros",
                        "ERROR",
                        JOptionPane.ERROR_MESSAGE);
            }
            
        }
        else if ( jb.getActionCommand ().equals ("eliminar") )
        {
            if( groupFileSelect == null )
            {
                JOptionPane.showMessageDialog (jFrameGroupFile,
                        "Debe seleccionar el grupo de ficheros a eliminar",
                        "ERROR",
                        JOptionPane.ERROR_MESSAGE);
            }
            else
            {
                int opcion = JOptionPane.showConfirmDialog (jFrameGroupFile,
                        "¿Esta seguro de eliminar este grupo de ficheros?",
                        "¿Seguro?",
                        JOptionPane.YES_NO_OPTION);
                
                if( opcion != 1 )
                {
                    new GroupFileDao ().deleteGroupFile ( groupFileSelect.getId () );

                    // Actualizamos la lista de categorias
                    Vector vGroupFile = new GroupFileDao ().selectGroupFile ();
                    if( vGroupFile.size () > 0 ) jFrameGroupFile.getListGroupFile ().setListData ( vGroupFile );

                    jFrameGroupFile.getTextFNameGroupFile ().setText ("");
                    jFrameGroupFile.getTextAExtensionGroupFile ().setText ("");
                }
            }
        }
        else if( jb.getActionCommand ().equals ("nuevo") )
        {
            groupFileSelect = null;
            jFrameGroupFile.getTextFNameGroupFile ().setText ("");
            jFrameGroupFile.getTextAExtensionGroupFile ().setText ("");
        }
    }
}